import { defineStore } from 'pinia'
import { getPixi, getPixiData } from '@renderer/store/modules/usePixiStore'
import * as PIXI from 'pixi.js'
import dayjs from 'dayjs'
import { excludeUnderscoreProps } from '@renderer/utils'
import useMagic from '@renderer/store/modules/useMagicStore'
import { PictureSourceEnums } from '@renderer/types/enums'

const initCreationData = (): Wx.Card.Data =>
  ({
    id: dayjs().unix(),
    author: 'mawby',
    name: '',
    picture: '',
    element: '金',
    series: '开天',
    rank: '凡',
    type: '生灵',
    category: '佛门',
    ability: '',
    consume: '',
    atk: '',
    hp: '',
    isDiy: true,
    transform: {
      minScale: 1,
      scale: 1,
      x: 0,
      y: 0,
      realWidth: 0,
      realHeight: 0,
      abilityFontSize: 28,
    },
  }) as Wx.Card.Data // 当前创作数据

const useDiyStore = defineStore('diy', {
  state: () => ({
    visible: false, // 是否显示 // TODO 设置默认是否显示DIY界面
    uidMap: {
      name: -1,
      consume: -1,
      atk: -1,
      hp: -1,
      ability: -1,
    } as Record<string, number>,
    optionUidMap: {
      name: -1,
      consume: -1,
      atk: -1,
      hp: -1,
      ability: -1,
      abilityFontSize: -1,
    } as Record<string, number>,
    creationData: JSON.parse(JSON.stringify(initCreationData())) as Wx.Card.Data,
    textSource: -1, // 文本来源
    imageUid: -1, // 图片容器uid
    improtData: false, // 导入标识
    portfolio: new Map() as Map<string, Wx.Card.Data[]>,
    editCardId: -1, // 正在编辑卡片的uid
    pictureSource: undefined as PictureSourceEnums | undefined,
    imageSource: {
      edit: 0, // 编辑
      import: 0, // 数据导入
      select: 0, // 选择图片
    },
  }),
  actions: {
    show() {
      this.visible = true
    },
    hidden() {
      this.visible = false
    },
    setCreationData(creationData: Wx.Card.Data) {
      this.creationData = creationData
    },
    setUidMap(name: string, uid: number) {
      this.uidMap[name] = uid
    },
    setOptionUidMap(name: string, uid: number) {
      this.optionUidMap[name] = uid
    },
    setVisible(visible: boolean) {
      this.visible = visible
    },
    reset() {
      // 重置数据
      this.creationData = JSON.parse(JSON.stringify(initCreationData()))
      // 重置选项文本部分
      Object.entries(this.optionUidMap).forEach(([, uid]) => {
        const text = getPixi(uid) as PIXI.Text
        const textData = getPixiData(uid)
        text.text = this.creationData[textData.attrName] || this.creationData.transform[textData.attrName] || ''
      })
      // 其他重置
      this.textSource = -1 // 文本来源
      this.improtData = false // 导入标识
      this.editCardId = -1 // 正在编辑卡片的uid
      // 清空图片容器
      const container = getPixi(this.imageUid)
      container.removeChildren()
    },
    newWork() {
      this.creationData.author = 'mawby' // TODO
      this.creationData.id = dayjs().unix()
    },
    // 将数据整理成保存格式
    dataCompleted() {
      // 保存位置信息
      const container = getPixi(this.imageUid)
      const sprite = container.children[0] as PIXI.Sprite
      if (sprite) {
        this.creationData.transform.scale = sprite.scale.x / useMagic().PICTURE_SCALE_COEFFICIENT
        this.creationData.transform.x = sprite.position.x
        this.creationData.transform.y = sprite.position.y
      }
      return excludeUnderscoreProps(this.creationData)
    },
    getReset() {
      return JSON.parse(JSON.stringify(initCreationData()))
    },
  },
})

export default useDiyStore
